package com.eyaomall.wxmall.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.AreaHelper;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.model.po.Address;
import com.eyaomall.wxmall.service.AddressService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.common.util.ExpressUtil;
import com.lycheeframework.core.exception.ErrorCodeException;
/**
 * 我的收货地址
 * @author pro
 *
 */
@Controller
@RequestMapping("address")
public class AddressController {
	
	@Autowired
	private AddressService addressService;
	
	/**
	 * 我的收货地址列表
	 * @param data
	 * @return
	 */
	@RequestMapping("index")
	public String index(Map<String,Object> data){
		data.put("aList", addressService.list(MemberHelper.getId()));
		
		return "address/index";
	}
	
	/**
	 * 我的收货地址详情
	 * @param id
	 * @param data
	 * @return
	 */
	@RequestMapping("detail")
	public String detail(Integer id,String json,String redirectUrl,Map<String,Object> data){
		if(id != null && id >= 0){
			data.put("address", addressService.queryById(id));
		}
		
		data.put("json", json);
		data.put("redirectUrl", redirectUrl);
		data.put("provinceList",AreaHelper.getProvinces());
		
		return "address/detail";
	}
	
	/**
	 * 删除我的地址
	 * @param id
	 * @return
	 */
	@RequestMapping("del")
	public @ResponseBody GsnData del(Integer id){
		if(id == null || id <= 0)
			throw new ErrorCodeException(BECode.address_300001);
		
		return GsnResponse.toData(addressService.deleteAddress(id));
	}
	
	/**
	 * 删除我的地址
	 * @param id
	 * @return
	 */
	@RequestMapping("set_default")
	public @ResponseBody GsnData setDefault(Integer id){
		if(id == null || id <= 0)
			throw new ErrorCodeException(BECode.address_300001);
		
		Address address = addressService.queryDefaultByMemberId(MemberHelper.getId());
		if(address != null && (address.getId() != id)){
			address.setIsDefault("n");
			addressService.updateAddress(address);
		}
		
		Address add = new Address();
		add.setId(id);
		add.setIsDefault("y");
		
		return GsnResponse.toData(addressService.updateAddress(add));
	}
	
	/**
	 * 保存我的收货地址
	 * @param address
	 * @return
	 */
	@RequestMapping("save")
	public @ResponseBody GsnData save(Address address){
		validate(address);
		
		boolean flag = false;
		
		address.setProvinceName(AreaHelper.getAreaName(address.getProvinceId()));
		address.setCityName(AreaHelper.getAreaName(address.getCityId()));
		address.setAreaName(AreaHelper.getAreaName(address.getAreaId()));
		if(address.getId() != null && address.getId() > 0){
			
			flag = addressService.updateAddress(address);
			return GsnResponse.toData(flag);
		}else{
			address.setMemberId(MemberHelper.getId());
			return GsnResponse.toData(addressService.createAddress(address));
		}
	}
	
	private void validate(Address address){
		if(StringUtil.isEmpty(address.getName()))
			throw new ErrorCodeException(BECode.address_300003);
		
		if(address.getName().length() > 20)
			throw new ErrorCodeException(BECode.address_300004);
		
		if(ExpressUtil.isNotPhone(address.getPhone()))
			throw new ErrorCodeException(BECode.address_300005);
		
		if(address.getProvinceId() == null || address.getProvinceId() <= 0)
			throw new ErrorCodeException(BECode.address_300006);
		
		if(address.getCityId() == null || address.getCityId() <= 0)
			throw new ErrorCodeException(BECode.address_300006);
		
		if(address.getAreaId() == null || address.getAreaId() <= 0)
			throw new ErrorCodeException(BECode.address_300006);
		
		if(StringUtil.isEmpty(address.getAddress()))
			throw new ErrorCodeException(BECode.address_300007);
		
		if(address.getAddress().length() > 60)
			throw new ErrorCodeException(BECode.address_300008);
		
		if(StringUtil.isNotEmpty(address.getZipCode()) && address.getZipCode().length() > 15)
			throw new ErrorCodeException(BECode.address_300009);
	}
}
